package l1j.server.server.clientpackets;

import java.util.logging.Logger;
import java.util.logging.Level;

import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.假人系統.DummyThread;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.L1ExcludingList;
import l1j.server.server.model.Instance.L1DummyInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_PacketBox;
import l1j.server.server.serverpackets.S_ServerMessage;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來封鎖密語的封包
 */
public class C_Exclude extends ClientBasePacket {

	private static final String C_EXCLUDE = "[C] C_Exclude";
	private static Logger _log = Logger.getLogger(C_Exclude.class.getName());

	@Override
	public String getType() {
		return C_EXCLUDE;
	}

	/**
	 * C_1 輸入 /exclude 指令的時候
	 */
	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}

			final String name = this.readS();
			if(name == null){
				return;
			}
			if (name.isEmpty()) {
				return;
			}

			try {
				final L1ExcludingList exList = pc.getExcludingList();
				if (exList.isFull()) {
					pc.sendPackets(new S_ServerMessage(472)); // 被拒絕的玩家太多。
					return;
				}
				final L1PcInstance traget =L1World.getInstance().getPlayer(name);
				final L1DummyInstance dummy = DummyThread.getInstance().getDummyTemp(name);
				if(traget == null && dummy == null){
					return;
				}
				int chaid = 0;
				if(traget !=null){
					chaid = traget.getId();
				}else{
					chaid = dummy.getId();
				}

				if (exList.contains(chaid)) {
					exList.remove(chaid);
					pc.sendPackets(new S_PacketBox(S_PacketBox.REM_EXCLUDE, name));
				} else {
					exList.add(chaid);
					pc.sendPackets(new S_PacketBox(S_PacketBox.ADD_EXCLUDE, name));
				}
			} catch (final Exception e) {
				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
